package com.kim.whereeat.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.kim.whereeat.model.StoreComment;
import com.kim.whereeat.utils.SQLiteHelper;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 伟阳 on 2015/10/24.
 */
public class StoreCommentService {
    private SQLiteHelper helper;
    private static final String TABLENAME = "storecomment";

    public StoreCommentService(Context context) {
        helper = new SQLiteHelper(context);
    }

    /**
     * 判断用户是否已评论
     *
     * @param user_id  用户ID
     * @param store_id 商店ID
     * @return true已评论/false未评论
     */
    public boolean exists(int user_id, int store_id) {
        boolean flag = false;
        SQLiteDatabase dbReader = helper.getReadableDatabase();
        Cursor cursor = dbReader.query(TABLENAME, null, "user_id=? and store_id=?", new String[]{user_id + "", store_id + ""}, null, null, null);
        flag = cursor.getColumnCount() > 0;
        cursor.close();
        return flag;
    }

    /**
     * 添加评论
     *
     * @param storeComment 评论
     * @return true评论成功/false评论失败
     */
    public boolean addComment(StoreComment storeComment) {
        SQLiteDatabase dbWriter = helper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("store_id", storeComment.getStore_id());
        cv.put("user_id", storeComment.getUser_id());
        cv.put("title", storeComment.getTitle());
        cv.put("content", storeComment.getContent());
        cv.put("time", storeComment.getTime());
        cv.put("star", storeComment.getStar());
        long temp = dbWriter.insert(TABLENAME, null, cv);
        return temp > 0;
    }

    /**
     * 获取食品平均评分
     *
     * @param store_id 商店ID
     * @return 食品平均评分
     */
    public double getAverageStarById(int store_id) {
        double avg = -1;
        SQLiteDatabase dbReader = helper.getReadableDatabase();
        Cursor cursor = dbReader.query(TABLENAME, new String[]{"AVG(star)"}, "store_id=?", new String[]{store_id + ""}, null, null, null);
        if (cursor.moveToNext()) {
            avg = cursor.getDouble(0);
        }
        cursor.close();
        return avg;
    }

    /**
     * 获取评论列表
     *
     * @param store_id 商店ID
     * @return 评论列表
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws NoSuchFieldException
     */
    public List<StoreComment> getstoreCommentListById(int store_id) throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        List<StoreComment> storeCommentList = new ArrayList<>();
        SQLiteDatabase dbReader = helper.getReadableDatabase();
        Cursor cursor = dbReader.query(TABLENAME, null, "store_id=?", new String[]{store_id + ""}, null, null, null);
        while (cursor.moveToNext()) {
            StoreComment storeComment = new StoreComment();
            storeComment.setId(cursor.getInt(0));
            storeComment.setStore_id(cursor.getInt(1));
            storeComment.setUser_id(cursor.getInt(2));
            storeComment.setTitle(cursor.getString(3));
            storeComment.setContent(cursor.getString(4));
            storeComment.setTime(cursor.getString(5));
            storeComment.setStar(cursor.getDouble(6));
            storeCommentList.add(storeComment);
        }
        cursor.close();
        return storeCommentList;
    }

    /**
     * 关闭数据库
     */
    public void close() {
        if (helper != null)
            helper.close();
    }
}
